package com.hongxun.design.jungong.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.jungong.fragment.FragmentMaterial;
import com.hongxun.design.jungong.model.MaterialDetail;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.ToastShow;

/**
 * Created by Administrator on 2017/10/27.
 */

public class FixMaterialFaultDialog extends DialogFragment {
    private AlertDialog dialog;
    private Handler handler;
    public static View view;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        view = inflater.inflate(R.layout.fixmaterialfaultdialog, null);
        ViewHolder holder = new ViewHolder(view);
        holder.findByIDs();
        dialog = builder.create();
        dialog.setView(view, 0, 0, 0, 0);
        return dialog;
    }

    private class ViewHolder {
        TextView tv1, tv2, tv3, tv4;
        EditText et1, et2, et3, et4, et5,et6;
        Button btn1, btn2;

        public ViewHolder(View view) {
            view.setTag(this);
        }

        public void findByIDs() {
            tv1 = (TextView) view.findViewById(R.id.dialog_tv_titlel);
            tv2 = (TextView) view.findViewById(R.id.dialog_tv_title);
            tv3 = (TextView) view.findViewById(R.id.dialog_tv_titler);
            tv4 = (TextView) view.findViewById(R.id.dialog_tv_result);

            et1 = (EditText) view.findViewById(R.id.create_et1);
            et2 = (EditText) view.findViewById(R.id.create_et2);
            et3 = (EditText) view.findViewById(R.id.create_et3);
            et4 = (EditText) view.findViewById(R.id.create_et4);
            et5 = (EditText) view.findViewById(R.id.create_et5);
            et6 = (EditText) view.findViewById(R.id.create_et31);
            btn1 = (Button) view.findViewById(R.id.create_btn_no);
            btn2 = (Button) view.findViewById(R.id.create_btn_yes);
            //关闭
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            setData(0);
        }

        private void setData(int index) {
            MaterialDetail detail = FragmentMaterial.errList.get(index);
            String name = detail.getName();
            if (name.length() > 20) name = name.substring(1, 12) + "...";
            tv2.setText("(" + (index + 1) + "/" + FragmentMaterial.errList.size() + ")\n" + name);
            tv4.setText("原因：" + FragmentMaterial.map.get(detail.getUuid()));
            et1.setText(detail.getMaterial_number());
            et2.setText(detail.getName());
            et3.setText(detail.getSpec());
            et4.setText(detail.getNum());
            et5.setText(detail.getUnit());
            et6.setText(detail.getType());
            //上一个
            tv1.setOnClickListener(new MClickListener(index, detail) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    if (position == 0) {
                        ToastShow.showShort(getActivity(), "已是第一个");
                        return;
                    }
                    setData(position - 1);
                }
            });
            //下一个
            tv3.setOnClickListener(new MClickListener(index, detail) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    if (position == FragmentMaterial.errList.size() - 1) {
                        ToastShow.showShort(getActivity(), "已是最后一个");
                        return;
                    }
                    setData(position + 1);
                }
            });
            //保存
            btn2.setOnClickListener(new MClickListener(index, detail) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    MaterialDetail detail = (MaterialDetail) msg;
                    String wlbm = String.valueOf(et1.getText()).trim();
                    String name = String.valueOf(et2.getText()).trim();
                    String num = String.valueOf(et4.getText()).trim();
                    String type=String.valueOf(et6.getText().toString()).trim();
                    if (wlbm.isEmpty()) {
                        ToastShow.showShort(getActivity(), "物料编码不能为空");
                        return;
                    }
                    if (type.isEmpty()) {
                        ToastShow.showShort(getActivity(), "分类不能为空");
                        return;
                    }
                    boolean isExist = false;
                    for (MaterialDetail detail1 : FragmentMaterial.yesList) {
                        if (wlbm.equals(detail1.getMaterial_number())) {
                            isExist = true;
                            break;
                        }
                    }
                    if (isExist) {
                        ToastShow.showShort(getActivity(), "物料编码重复");
                        return;
                    }
                    if (name.isEmpty()) {
                        ToastShow.showShort(getActivity(), "名称不能为空");
                        return;
                    }
                    if (num.isEmpty()) {
                        ToastShow.showShort(getActivity(), "数量不能为空");
                        return;
                    } else {
                        if (num.contains(".")) {
                            String[] split = num.split("\\.");
                            if (TextUtils.isEmpty(split[0])) {
                                ToastShow.showShort(getActivity(), "数量格式错误");
                                return;
                            }
                            if (TextUtils.isEmpty(split[1])) {
                                ToastShow.showShort(getActivity(), "数量格式错误");
                                return;
                            }
                        } else {
                            if (!TextUtils.isDigitsOnly(num)) {
                                ToastShow.showShort(getActivity(), "数量格式错误");
                                return;
                            }
                        }
                    }
                    if (num.trim().isEmpty()) {
                        num = "0";
                    } else {
                        if (!num.contains(".")) num = String.valueOf(Integer.valueOf(num)).trim();
                    }
                    detail.setMaterial_number(wlbm);
                    detail.setName(name);
                    detail.setType(type);
                    detail.setSpec(String.valueOf(et3.getText()).trim());
                    detail.setNum(num);
                    detail.setUnit(String.valueOf(et5.getText()).trim());
                    Message message = handler.obtainMessage();
                    message.obj = detail;
                    message.what = 765;
                    handler.sendMessage(message);

                    FragmentMaterial.yesList.add(detail);
                    FragmentMaterial.errList.remove(msg);
                    FragmentMaterial.map.remove(((MaterialDetail) msg).getMaterial_number());

                    if (FragmentMaterial.errList.size() == 0) dialog.dismiss();
                    else {
                        if (position == FragmentMaterial.errList.size()) {//切换展示前移1位数据
                            setData(position - 1);
                        } else if (position < FragmentMaterial.errList.size()) {//切换展示后移1位数据展示
                            setData(position);
                        }
                    }
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        view = null;
        super.onDestroyView();
    }
}
